﻿<?php
/*
	积分兑奖
	author:江小鱼
	website:http://u.i0580.com
	description:本插件业余时间完成 可能还存在不足和BUG 希望大家一起完善他 本人崇尚的是Free && Share 希望得到代码的人也能和其他人分享
*/

if(!defined('IN_UCHOME') || !defined('IN_ADMINCP'))
{
	exit('Access Denied');
}

//权限
if(!checkperm('managead')) 
{
	cpmessage('no_authority_management_operation');
}

if(empty($_GET['op']))
{
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('award_info')." ORDER BY orders ASC");
	while ($awardlist = $_SGLOBAL['db']->fetch_array($query)) 
	{
		$award[] = $awardlist;		
	}
}
elseif ($_GET['op'] == 'edit' || $_GET['op']=='add')
{
	if(submitcheck('awardsubmit')) 
	{
		include_once(S_ROOT.'./source/function_blog.php');
		$award_id = isset($_POST['award_id']) ? intval($_POST['award_id']) : 0;
		$award_name = trim($_POST['award_name']);
		$award_description = trim($_POST['award_description']);
		$award_awardno = trim($_POST['award_awardno']);
		$award_credit = intval($_POST['award_credit']);
		$award_orders = intval($_POST['award_orders']);
		$award_isopen = intval($_POST['award_isopen']);
		$award_awardimg = trim($_POST['award_awardimg']);
		$award_description = checkhtml($award_description);
		$award_description = getstr($award_description, 0, 1, 0, 1, 0, 1);
		$award_description = preg_replace("/\<div\>\<\/div\>/i", '', $award_description);

		if(empty($award_name))
		{
			cpmessage('奖品名称不能为空！');
		}

		if(empty($award_description))
		{
			cpmessage('奖品描述不能为空！');
		}

		if(empty($award_awardno))
		{
			cpmessage('奖品编号不能为空！');
		}

		if(empty($award_credit) || $award_credit==0)
		{
			cpmessage('兑换积分必须大于零！');
		}

		if(empty($award_orders))
		{
			$award_orders = 0;
		}

		if(empty($award_awardimg))
		{
			cpmessage('奖品图片不能为空！');
		}

		if($award_id == 0)
		{
			$query = $_SGLOBAL['db']->query("INSERT INTO ".tname('award_info')." (awardno,awardname,credit,isopen,awardimg,description,orders ) VALUES ('$award_awardno','$award_name','$award_credit','$award_isopen','$award_awardimg','$award_description','$award_orders')");
			$_act = '添加';
		}
		else
		{
			$query = $_SGLOBAL['db']->query("UPDATE ".tname('award_info')." SET awardno='$award_awardno',awardname='$award_name',credit='$award_credit',isopen='$award_isopen',awardimg='$award_awardimg',description='$award_description',orders='$award_orders' WHERE id='$award_id'  LIMIT 1");

			$_SGLOBAL['db']->query("UPDATE ".tname('award')." SET awardname='$award_name',credit='$award_credit',awardno='$award_awardno' WHERE aid='$award_id'");

			$_act = '更新';
		}
		
		if($query)
		{
			cpmessage("奖品{$_act}成功！",'admincp.php?ac=award',1);
		}
		else
		{
			cpmessage("奖品{$_act}失败！",'admincp.php?ac=award',1);
		}
	}
	elseif(intval($_GET['id']) !=0)
	{
		$id = intval($_GET['id']);
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('award_info')." WHERE id='$id'");
		$award = $_SGLOBAL['db']->fetch_array($query); 		
	}
}
elseif($_GET['op'] == 'del')
{
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	if($_POST['ids'])
	{
		$_SGLOBAL['db']->query("DELETE FROM ".tname('award_info')." WHERE id IN (".simplode($_POST['ids']).")");
		$_SGLOBAL['db']->query("DELETE FROM ".tname('award')." WHERE aid IN (".simplode($_POST['ids']).")");
	}
	elseif($id>0)
	{
		$_SGLOBAL['db']->query("DELETE FROM ".tname('award_info')." WHERE id ='$id'");
		$_SGLOBAL['db']->query("DELETE FROM ".tname('award')." WHERE aid ='$id'");
	}
	cpmessage('do_success', 'admincp.php?ac=award', 1);	
}
elseif($_GET['op'] == 'delp')
{
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	if($_POST['ids'])
	{
		$_SGLOBAL['db']->query("DELETE FROM ".tname('award')." WHERE id IN (".simplode($_POST['ids']).")");
	}
	elseif($id>0)
	{
		$_SGLOBAL['db']->query("DELETE FROM ".tname('award')." WHERE id ='$id'");
	}
	cpmessage('do_success', 'admincp.php?ac=award', 1);	
}
elseif($_GET['op'] == 'list')
{
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('award')."  ORDER BY id DESC");
	while ($list = $_SGLOBAL['db']->fetch_array($query)) 
	{
		$plist[] = $list;		
	}

}
elseif($_GET['op'] =='editp')
{
	if(submitcheck('awardpsubmit')) 
	{
		$id = intval($_POST['id']);
		$aid =  intval($_POST['aid']);
		$oid =  intval($_POST['oid']);
		$card =   trim($_POST['card']);
		$pwd =   trim($_POST['pwd']);
		
		if($aid == 0)
		{
			cpmessage('奖品分类不能空', 'admincp.php?ac=award&op=editp', 1);
		}

		if(strlen($card)<6 || strlen($card)>32)
		{
			cpmessage('卡号长度范围6-32', 'admincp.php?ac=award&op=editp', 1);
		}

		if(strlen($pwd)<4 || strlen($pwd)>32)
		{
			cpmessage('密码长度范围4-32', 'admincp.php?ac=award&op=editp', 1);
		}

		$ainfo = getAwardInfo($aid);
		$awardname = $ainfo['awardname'];
		$credit = $ainfo['credit'];
		$awardno = $ainfo['awardno'];

		$query = $_SGLOBAL['db']->query("UPDATE ".tname('award')." SET aid='$aid',card='$card',pwd='$pwd',awardname='$awardname',credit='$credit',awardno='$awardno' WHERE id='$id' LIMIT 1");

		if($query)
		{
			if($aid!=$oid)
			{
				AddAwardInfoNum($aid);
				SubAwardInfoNum($oid);
			}
			cpmessage('do_success', 'admincp.php?ac=award&op=list', 1);
		}
		else
		{
			cpmessage('do_success', 'admincp.php?ac=award&op=list', 1);
		}
	}
	else
	{
		$id = intval($_GET['id']);

		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('award')." WHERE id='$id'");

		$award = $_SGLOBAL['db']->fetch_array($query);		
	}

}
elseif($_GET['op'] == 'addp')
{
	if(submitcheck('awardpsubmit')) 
	{
		$id = intval($_POST['id']);
		$aid =  intval($_POST['aid']);
		$card =   trim($_POST['card']);
		$pwd =   trim($_POST['pwd']);
		
		if($aid == 0)
		{
			cpmessage('奖品分类不能空', 'admincp.php?ac=award&op=list', 1);
		}

		if(strlen($card)<6 || strlen($card)>32)
		{
			cpmessage('卡号长度范围6-32', 'admincp.php?ac=award&op=list', 1);
		}

		if(strlen($pwd)<4 || strlen($pwd)>32)
		{
			cpmessage('密码长度范围4-32', 'admincp.php?ac=award&op=list', 1);
		}

		$ainfo = getAwardInfo($aid);
		$awardname = $ainfo['awardname'];
		$credit = $ainfo['credit'];
		$awardno = $ainfo['awardno'];

		$query = $_SGLOBAL['db']->query("INSERT INTO ".tname('award')." SET aid='$aid',card='$card',pwd='$pwd',awardname='$awardname',credit='$credit',awardno='$awardno'");
		if($query)
		{
			AddAwardInfoNum($aid);
			cpmessage('do_success', 'admincp.php?ac=award&op=list', 1);
		}
		else
		{
			cpmessage('do_success', 'admincp.php?ac=award&op=list', 1);
		}
	}
}

function getMyClass($aid=0)
{
	global $_SGLOBAL;
	$tmpstr = '';
	$query = $_SGLOBAL['db']->query("SELECT id,awardname FROM ".tname('award_info')." WHERE isopen=1 ORDER BY orders ASC");
	while($list = $_SGLOBAL['db']->fetch_array($query))
	{
		if($aid == $list['id'])
		{
			$tmpstr .= "<OPTION VALUE='".$list['id']."' SELECTED>".$list['awardname']."</OPTION>\r\n";
		}
		else
		{
			$tmpstr .= "<OPTION VALUE='".$list['id']."'>".$list['awardname']."</OPTION>\r\n";
		}
	}
	return $tmpstr;
}

function getAwardInfo($aid)
{
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('award_info')." WHERE isopen=1 AND id='$aid'");
	$list = $_SGLOBAL['db']->fetch_array($query);
	return $list;
}

//增加奖品数量
function AddAwardInfoNum($id)
{
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query("UPDATE ".tname('award_info')." SET remain=remain+1 WHERE id='$id' LIMIT 1");
}

//减少奖品数量
function SubAwardInfoNum($id)
{
	global $_SGLOBAL;
	$query = $_SGLOBAL['db']->query("UPDATE ".tname('award_info')." SET remain=remain-1 WHERE id='$id' LIMIT 1");
}
?>